define(function(require, exports, module) {
	function beautify (obj){
		if(obj.type.toLowerCase()=='checkbox'){
			this.beautifyCheckbox(obj);
		}else if(obj.type.toLowerCase()=='radio'){
			this.beautifyRadio(obj);
		}else if(obj.type.toLowerCase().substring(0,6)=='select'){
			this.beautifySelect(obj);		
		}
	}
	beautify.prototype.buildSelectTpl = function(v){
		var html = '',selText='';
		$(v).find('option').each(function(key,value){
			if(value.selected){
				selText = value.innerHTML;
				html +='<li><a href="javascript:;" class="checked">'+selText+'</a></li>';
			}else{
				html +='<li><a href="javascript:;">'+value.innerHTML+'</a></li>';
			}
		});
		return [html,selText];
	}
	beautify.prototype.refreshSelect = function(v){
		var html = this.buildSelectTpl(v);
		$(v).siblings('ul').html(html[0]);
		$(v).siblings('.selText').html(html[1]);
	}
	beautify.prototype.beautifySelect = function(v){
		if(1==$(v).attr('beautify')){
			this.refreshSelect(v);
		}else{
			$(v).wrap('<span class="wSelect"></span>');
			var n = $(v).parent();
			n.prepend('<div class="selArrow"><i></i></div><div class="selText"></div><ul></ul>');
			var selText = n.find('.selText'),
				arrow = n.find('.selArrow'),
				select = n.find('select'),
				list = n.find('ul');
			this.refreshSelect(v);
			n.hover(function(){list.show();},function(){list.hide();})
			list.delegate('a','click',function() {
				var i =list.find('a').index($(this));
				var selected = $(n).find('option').eq(i);
				//console.info(selected);
				selected.attr('selected' ,true);
				selText.html(selected.text());
				list.hide();
				select.trigger('change');
			});
			//SSSUI.addSpaceClose($('.wSelect'),function(){list.hide();});
			$(select).attr('beautify',1);
		}
	}
	beautify.prototype.beautifyCheckbox = function(v){
		var name = $(v).attr('name'),className = 'wCheckbox',itext='&#xf096;',no=$('[name='+name+'][no]').length;
		$(v).attr({'id':name+'_'+no,no:no});
		if(v.checked){className += ' checked';itext='&#xf046;';}
		if($(v).parent('label')){
			$(v).parent('label').addClass(className).attr('for',name+'_'+no);
		}else{
			$(v).wrap('<label for="'+name+'_'+no+'" class="'+className+'"></label>');
		}
		$(v).before('<i>'+itext+'</i>');
		$(v).change(function(){
			$(this).parent().toggleClass('checked');
			var i = $(this).parent().find('i');
			//console.info(i);
			if(i.html()==String.fromCharCode(0xf046)){
				i.html('&#xf096;');
			}else{
				i.html('&#xf046;');				
			}
		});
	}
	beautify.prototype.beautifyRadio = function(v){
		if(1==$(v).attr('beautify')){return ;}
		var name = $(v).attr('name'),className = '',itext='&#xf10c;',no=$('[name='+name+'][no]').length;
		if($('[name='+name+'][data-type=switch]').length==0){
			className = 'wRadio';
			$(v).attr({'id':name+'_'+no,no:no});
			if(v.checked) {className += ' checked';itext='&#xf111;';};
			if($(v).parent('label')){
				$(v).parent('label').addClass(className).attr('for',name+'_'+no);
			}else{
				$(v).wrap('<label for="'+name+'_'+no+'" class="'+className+'"></label>');
			}
			$(v).before('<i>'+itext+'</i>');
			$(v).change(function(){
				//alert(name);
				$(':radio[name='+name+']').parent('.wRadio').removeClass('checked').find('i').html('&#xf10c;');
				$(this).parent('.wRadio').addClass('checked').find('i').html('&#xf111;');
			})
		}else{
			$('[name='+name+'][data-type=switch]').each(function(k,v){
				$(v).attr({'id':name+'_'+no,no:no});
				className = 'wSwitch';
				if(v.checked) {className += ' checked';itext='&#xf111;'};
				if($(v).parent('label')){
					$(v).parent('label').addClass(className).attr('for',name+'_'+no);
				}else{
					$(v).wrap('<label for="'+name+'_'+no+'" class="'+className+'"></label>');
				}
				no++;
				$(v).attr('beautify',1);
			});
			//$(v).before('<i>'+itext+'</i>');
			$(':radio[name='+name+']').parent('.wSwitch').wrapAll('<div class="wSwitchGroup"></div>');
			$(':radio[name='+name+']').change(function(){
				$(':radio[name='+name+']').parent('.wSwitch').removeClass('checked').find('i').html('&#xf10c;');
				$(this).parent('.wSwitch').addClass('checked').find('i').html('&#xf111;');
			})
		}
	}
	$.fn.extend({
		beautify:function(){
			$(this).each(function(k,v){new beautify(v);});
		}
	});
	exports.beautify = function (n){
		$(n).each(function(k,v){new beautify(v);console.info(v)});
	}
});
	
